var pager=1,oneID='',twoID='',threeID='',noDatePager=true,sort='popularity';
$(document).ready(function(){
	getCategory();//获取分类名
})
//获取分类名方法
function getCategory(){
	$('.content.vertical-list ul').html('');
	$('.countDisplay em').html('0')
	var category=JSON.parse(sessionStorage.getItem("category_Arr"));
	if(category != undefined && category !="" && category != null){
		var navigation='';
		for (var i=0;i<category.length;i++) {
			if (i==0) {
				oneID=category[i].id;
				navigation+='<a href="javascript:void(0)" class="fresh-fruits mui-ellipsis" data-id="'+category[i].id+'">'+
					'<img src="'+category[i].url+'" />'+category[i].name+
					'</a>';
			} else if(i==1){
				twoID=category[i].id;
				navigation+='<a href="javascript:void(0)" class="two-fruits mui-ellipsis" data-id="'+category[i].id+'">'+
					'<span>&middot;</span>'+category[i].name+
					'</a>';
			} else if (i==2){
				threeID=category[i].id;
				navigation+='<a href="javascript:void(0)" class="three-fruits mui-ellipsis" data-id="'+category[i].id+'">'+
					'<span>&middot;</span>'+category[i].name+
					'</a>';
			}
		}
		$('.classify-name').html(navigation);
		set_map_width();//设置菜单宽度
		$('.fresh-fruits').on('tap',function(){
			var category_Arr =JSON.parse(sessionStorage.getItem('category_Arr'));
			category_Arr.splice(1,2);
			sessionStorage.setItem('category_Arr',JSON.stringify(category_Arr));
			location.href=MPJ.base+"/pages/product/product-list.jsp";
		})
		$('.two-fruits').on('tap',function(){
			var category_Arr =JSON.parse(sessionStorage.getItem('category_Arr'));
			category_Arr.splice(2,1);
			sessionStorage.setItem('category_Arr',JSON.stringify(category_Arr));
			location.href=MPJ.base+"/pages/product/product-list.jsp";
		})
		
		
		
		getCategory_sub();//获取子类菜单方法		
		getInit();
	
	}else{
		getCategory();//获取分类名
	}
}

function set_map_width(){
	var sWidth=$('.classify-name').width();
	var len=$('.classify-name a').length;
	var len1='',len2='',len3='';
	for (var i=0;i<len;i++) {
		if(i==0){
			len1=$('.classify-name a.fresh-fruits').width();
		}else if(i==1){
			len2=$('.classify-name a.two-fruits').width();
		}else if(i==2){
			len3=$('.classify-name a.three-fruits').width();
		}
	}
	if(len==2){
		if((len1+len2+60)>sWidth){
			$('.classify-name a.fresh-fruits').css("width",(sWidth-len2-30));
		}
	}
	if(len==3){
		if((len1+len2+len3+35)>sWidth){
			$('.classify-name a.fresh-fruits').css("width",(sWidth-len3-35)/2);
			$('.classify-name a.two-fruits').css("width",(sWidth-len3-35)/2);
		}
	}
}

//获取子类菜单方法
function getCategory_sub(){
	var classify_Arr =JSON.parse(sessionStorage.getItem('classify_Arr'));
	var select_option_str='';
	if(classify_Arr!=undefined && classify_Arr != "" && classify_Arr != null){
		for(var i=0;i<classify_Arr.length;i++){
			if(oneID==classify_Arr[i].department_id){
				var aisle=classify_Arr[i].aisle;
				for(var j=0;j<aisle.length;j++){
					if(twoID==""){
						select_option_str+='<option value="'+aisle[j].aisle_id+'">'+aisle[j].aisle_name+'</option>';
					}else if(twoID==aisle[j].aisle_id){
						var shelf=aisle[j].shelf;
						select_option_str='';
						for(var k=0;k<shelf.length;k++){
							select_option_str+='<option value="'+shelf[k].shelf_id+'">'+shelf[k].shelf_name+'</option>';
						}
					}
				}
			}
		}	
		$('#refine').html('<option value="">Refine</option>'+select_option_str);
	}
}


//获取列表
function getProductList(pager){ 
	var arr={
		hs_dept_id:oneID,
		hs_aisle_id:twoID,
		hs_shelf_id:threeID,
		hs_rec_per_page:10,
		hs_srch_page_no:pager,
		hs_sort:sort,
		hs_action_id:"submit",
		hf_s_id:"WD11",
		hf_srv_id:"Pv_jpdt_brw",
		hs_offer_only:"N",
		hf_locale_id:"en"
	};
	getAjaxRequest(arr,function(result){
		var productArr=setProductList_ver(result);
		if(productArr[1]==0){
			noDatePager=false;
			$('.content.vertical-list ul').append("<li class='nodate'>No more data</li>");
		}else{
			$('.content.vertical-list ul').append(productArr[0]);
		}

		$('#offCanvasContentScroll .mui-scroll').css('transform', 'translate3d(0px, 0px, 0px) translateZ(0px)');
		mui('.mui-numbox').numbox();

		var num=parseInt($('.countDisplay em').html());
		$('.countDisplay em').html(productArr[1]+num);
		$('.pageNum').html('Currently displaying 1-'+pager+' of '+(productArr[1]+num)+' products');
		
		//两列数据的列表点击
		$('.vertical-list li').each(function(){
			var $this=$(this);
			$this.on('tap','.imgBox',function(){
				var pdt_id=$this.data('id');
				var item_no=$this.data('item-no');
				var product_info={};
				product_info.pdt_id=pdt_id;
				product_info.item_no=item_no;
				sessionStorage.setItem('product_info',JSON.stringify(product_info));
				location.href=MPJ.base+"/pages/product/product-detail.jsp";
			})
			$this.on('tap','.addCar',function(){
				var pdt_id=$this.data('id');
				var item_no=$this.data('item-no');
				var arr={
					hf_s_id:"WD11",
					hf_srv_id:"Av_jcart",
					hs_action_id:"add_all_to_cart",
					hvs_add_pdt_id:pdt_id,
					hvs_add_qty:$this.find('.mui-numbox-input').val(),
				};
				getAjaxRequest(arr,function(result){
					if(result.bs_result==BS_RESULT.OK){
						dialog('Message', 'Add to Shopping Cart', 1);
						var $dialog = $('#dialog');
						$dialog.show();
						$dialog.find('.comfirmBtn').one('click', function() {
							$dialog.hide();
							$dialog.remove();
							getCartSumCount();
						})
					}

				});

			})
		})
		
		
	});
}


//选择分类列表进行分类获取
$('#refine').change(function(){
	var id=$(this).children('option:selected').val();
	var name=$(this).children('option:selected').html();
	var category_Arr =JSON.parse(sessionStorage.getItem('category_Arr'));
	if(id!=""){
		if(category_Arr.length==1){
			var aisle_classify={};
			aisle_classify.id=id;
			aisle_classify.name=name;
			category_Arr.push(aisle_classify);
		}else if(category_Arr.length==2){
			var shelf_classify={};
			shelf_classify.id=id;
			shelf_classify.name=name;
			category_Arr.push(shelf_classify);	
		}else if(category_Arr.length==3){
			category_Arr[2].id=id;
			category_Arr[2].name=name;			
		}
	}
	sessionStorage.setItem('category_Arr',JSON.stringify(category_Arr));
	
	getCategory();
	getProductList(1);
})
$('#sort').change(function(){
	sort=$(this).children('option:selected').val();
	getCategory();
	getProductList(1);
})
//数量减少
/*$('.mui-numbox-btn-minus').each(function(index){
	$(this).click(function(){
		var num=parseInt($('.mui-numbox-input').eq(index).val());
		num=num-1;
		if(num==0){
			num=1;
		}
		$('.mui-numbox-input').eq(index).val(num);
	})
})*/

//数量增加
/*$('.mui-numbox-btn-plus').each(function(index){
	$(this).click(function(){
		var num=parseInt($('.mui-numbox-input').eq(index).val());
		num=num+1;
		$('.mui-numbox-input').eq(index).val(num);
	})
})*/







function getInit(){
	mui.init({
		pullRefresh: {
			container: '#offCanvasContentScroll',
			up: {
				contentrefresh: '正在加载...',
				callback: pullupRefresh
			}
		}
	});
}

/**
 * 上拉加载具体业务实现
 */
function pullupRefresh() {
	setTimeout(function() {
		mui('#offCanvasContentScroll').pullRefresh().endPullupToRefresh((noDatePager==false)); //参数为true代表没有更多数据了。
		if(noDatePager){
			getProductList(pager);
			pager++;		
		}
	}, 1000);
}
if (mui.os.plus) {
	mui.plusReady(function() {
		setTimeout(function() {
			mui('#offCanvasContentScroll').pullRefresh().pullupLoading();
		}, 1000);

	});
} else {
	mui.ready(function() {
		mui('#offCanvasContentScroll').pullRefresh().pullupLoading();
	});
}




